<?php

namespace ShowStop\TelevisionBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class MainController extends Controller
{
    
    public function homepageAction() {
		//get doctrine entity manager
		$em = $this->getDoctrine()->getEntityManager();
		
		$query = $em
				->createQuery( "SELECT s FROM ShowStopTelevisionBundle:Series s WHERE s.return_date >= '" . date("Y-m-d") . "' ORDER BY s.return_date ASC" )
				->setMaxResults(10);
		
		$returningShows = $query->getResult();
		
		$date = strtotime( '-1 month', strtotime( date("Y-m-d") ) );		
		$date = date( "Y-m-d", $date );
		
		$query = $em
				->createQuery( "SELECT s FROM ShowStopTelevisionBundle:Series s WHERE s.return_date < '" . date("Y-m-d") . "' AND s.return_date >= '" . $date . "'  ORDER BY s.return_date ASC" )
				->setMaxResults(10);
		
		$recentShows = $query->getResult();
		
		if (!$returningShows || !$recentShows) {
		throw $this->createNotFoundException( "No television show found called '" . $name . "'");
		}
		
		//create the search form
		$form = $this->createFormBuilder( array() )
			->add('searchBox', 'text')
			->getForm();
		
        return $this->render('ShowStopTelevisionBundle:Main:index.html.twig', array('returningShows' => $returningShows, 'recentShows' => $recentShows, 'searchForm' => $form->createView() ) );
    }
    
    
    	
}
